using GalaSoft.MvvmLight;

namespace Tombstoner.ViewModel
{
    using System.Collections.Generic;

    using GalaSoft.MvvmLight.Messaging;
    using GalaSoft.MvvmLight.Threading;

    using Tombstoner.Model;

    using WP7Contrib.Logging;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.Services.Storage;

    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBaseWp7
    {
        private string textBoxValue;

        private string textBox1Value;

        private bool checkBoxValue;

        private string passwordBoxValue;

        private string passwordBox1Value;

        private bool radioButtonValue;

        private bool radioButton1Value;

        private bool radioButton2Value;

        private int sliderValue;

        private int slider1Value;

        private int slider2Value;

        private bool toggleButtonValue;

        private bool toggleButton1Value;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(
            INavigationService navigationService,
            IMessenger messenger,
            IStorageService storageService,
            ILog log)
            : base(navigationService, storageService, messenger, log)
        {
        }

        public string TextBoxValue
        {
            get
            {
                return this.textBoxValue;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.textBoxValue, value, "TextBoxValue");
            }
        }

        public string TextBox1Value
        {
            get
            {
                return this.textBox1Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.textBox1Value, value, "TextBox1Value");
            }
        }

        public bool CheckBoxValue
        {
            get
            {
                return this.checkBoxValue;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.checkBoxValue, value, "CheckBoxValue");
            }
        }

        public string PasswordBoxValue
        {
            get
            {
                return this.passwordBoxValue;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.passwordBoxValue, value, "PasswordBoxValue");
            }
        }

        public string PasswordBox1Value
        {
            get
            {
                return this.passwordBox1Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.passwordBox1Value, value, "PasswordBox1Value");
            }
        }

        public bool RadioButtonValue
        {
            get
            {
                return this.radioButtonValue;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.radioButtonValue, value, "RadioButtonValue");
            }
        }

        public bool RadioButton1Value
        {
            get
            {
                return this.radioButton1Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.radioButton1Value, value, "RadioButton1Value");
            }
        }

        public bool RadioButton2Value
        {
            get
            {
                return this.radioButton2Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.radioButton2Value, value, "RadioButton2Value");
            }
        }

        public int SliderValue
        {
            get
            {
                return this.sliderValue;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.sliderValue, value, "SliderValue");
            }
        }

        public int Slider1Value
        {
            get
            {
                return this.slider1Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.slider1Value, value, "Slider1Value");
            }
        }

        public int Slider2Value
        {
            get
            {
                return this.slider2Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.slider2Value, value, "Slider2Value");
            }
        }

        public bool ToggleButtonValue
        {
            get
            {
                return this.toggleButtonValue;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.toggleButtonValue, value, "ToggleButtonValue");
            }
        }

        public bool ToggleButton1Value
        {
            get
            {
                return this.toggleButton1Value;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.toggleButton1Value, value, "ToggleButton1Value");
            }
        }

        public List<PeopleInGroup> Buddies
        {
            get
            {
                return new PeopleByFirstName();
            }
        }

        protected override void IsBeingActivated(IStorage storage)
        {
            var tbv = storage.Read<string>("TextBoxValue");
            var tbv1 = storage.Read<string>("TextBox1Value");
            var cbv = storage.Read<bool>("CheckBoxValue");
            var pbv = storage.Read<string>("PasswordBoxValue");
            var pbv1 = storage.Read<string>("PasswordBox1Value");
            var rbv = storage.Read<bool>("RadioButtonValue");
            var rbv1 = storage.Read<bool>("RadioButton1Value");
            var rbv2 = storage.Read<bool>("RadioButton2Value");
            var sv = storage.Read<int>("SliderValue");
            var sv1 = storage.Read<int>("Slider1Value");
            var sv2 = storage.Read<int>("Slider2Value");
            var tgbv = storage.Read<bool>("ToggleButtonValue");
            var tgbv1 = storage.Read<bool>("ToggleButton1Value");

            DispatcherHelper.CheckBeginInvokeOnUI(
               () =>
               {
                   this.TextBoxValue = tbv;
                   this.TextBox1Value = tbv1;
                   this.CheckBoxValue = cbv;
                   this.PasswordBoxValue = pbv;
                   this.PasswordBox1Value = pbv1;
                   this.RadioButtonValue = rbv;
                   this.radioButton1Value = rbv1;
                   this.radioButton2Value = rbv2;
                   this.SliderValue = sv;
                   this.Slider1Value = sv1;
                   this.Slider2Value = sv2;
                   this.ToggleButtonValue = tgbv;
                   this.ToggleButton1Value = tgbv1;

                   storage.Clear();
               });
        }

        protected override void IsBeingDeactivated(IStorage storage)
        {
            storage.Write("TextBoxValue", this.TextBoxValue);
            storage.Write("TextBox1Value", this.TextBox1Value);
            storage.Write("CheckBoxValue", this.CheckBoxValue);
            storage.Write("PasswordBoxValue", this.PasswordBoxValue);
            storage.Write("PasswordBox1Value", this.PasswordBox1Value);
            storage.Write("RadioButtonValue", this.RadioButtonValue);
            storage.Write("RadioButton1Value", this.RadioButton1Value);
            storage.Write("RadioButton2Value", this.RadioButton2Value);
            storage.Write("SliderValue", this.SliderValue);
            storage.Write("Slider1Value", this.Slider1Value);
            storage.Write("Slider2Value", this.Slider2Value);
            storage.Write("ToggleButtonValue", this.ToggleButtonValue);
            storage.Write("ToggleButton1Value", this.ToggleButton1Value);
        }
    }
}